// /home/san/src/projects/sanfs/MediaChanger/DataStoreDriver/XmlDataStoreDriver.cs created with MonoDevelop
// User: san at 8:20 PM 5/27/2007
//
// A simple XML based storage backend for the MediaChanger.
// 
//

using System;
using System.Xml;
using System.Collections.Generic;
using SanFS.DiscLibrary;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;


namespace SanFS.MediaChangerLibrary
{
	public class XmlDataStoreDriver : IDataStore
	{
		private string			mUri = null;
		private BinaryFormatter	mFmt = new BinaryFormatter();
		
		public XmlDataStoreDriver()
		{
		}
		
		public XmlDataStoreDriver(string Uri)
		{
			mUri = Uri.Split(':')[2];
		}

		public IDataStore CreateInstance(string Uri)
		{
			return new XmlDataStoreDriver(Uri);
		}
		
		public virtual void Initialize()
		{
		}
		
		public virtual void SaveDiscList(List<IDisc> Discs)
		{
			FileStream	fs = new FileStream(mUri + ".dl", FileMode.Create);
			try
			{
				mFmt.Serialize(fs, Discs);
			}
			catch
			{
				throw;
			}
			finally
			{
				fs.Close();
			}
		}

		public virtual List<IDisc> GetDiscList()
		{
			FileStream	fs = new FileStream(mUri + ".dl", FileMode.Open);
			try
			{
				return (List<IDisc>) mFmt.Deserialize(fs);
			}
			catch
			{
				throw;
			}
			finally
			{
				fs.Close();
			}
		}

		public virtual void SaveStorageElementList(List<IStorageElement> StorageElements)
		{
			FileStream	fs = new FileStream(mUri + ".sl", FileMode.Create);
			try
			{
				mFmt.Serialize(fs, StorageElements);
			}
			catch
			{
				throw;
			}
			finally
			{
				fs.Close();
			}
		}

		public virtual List<IStorageElement> GetStorageElementList()
		{
			FileStream	fs = new FileStream(mUri + ".sl", FileMode.Open);
			try
			{
				return (List<IStorageElement> ) mFmt.Deserialize(fs);
			}
			catch
			{
				throw;
			}
			finally
			{
				fs.Close();
			}
		}

		public virtual void Disconnect()
		{
		}

		public virtual void Connect()
		{
			// Make sure the URI is valid and writeable
		}
		
		
		public string Uri
		{
			get { return this.mUri; }
		}
		
		public string Key
		{
			get
			{
				return this.GetType().Name;
			}
		}
	} // END CLASS
}  // END NAMESPACE
